var utils = require('../../../utils/utils.js'),
appData = getApp().globalData, app = getApp(),
app = getApp();
Page({
  data: {
    loading: true,
    footTitle: '暂未领取优惠券',
  },
  tap: function(e) {
    console.log(e)
    if (e.currentTarget.id[0] == 0) { 
      app.toast('折抵券已经使用过了哦'); 
      return 0;
    }
    var n = e.currentTarget.id.substring(1),
    info = this.data.intro[n.split('%')[0]];
    console.log(n)
    wx.navigateTo({ url: 'code?code=' + n.split('%')[1] + '%' + n.split('%')[2] + '&info=' + JSON.stringify(info)});
  },
  onLoad: function (op) {
    var that = this, intro = [];
    var GT_data = { openid: appData.user.openid };
    app.request('receive/getTicket', GT_data, '查询中').then((res) => {
      console.log(res.name, res);
      for (let i = 0; i < res.data.length; i++) {
        var giv = res.data[i].openid + res.data[i].openid,
        rec = appData.user.openid + appData.user.openid,
        rect = parseInt(new Date().getTime() / 1000);
        intro.push({
          array: i,
          id: res.data[i].id,
          src: res.data[i].nick,
          share_id: res.data[i].share_id,
          total: res.data[i].total,
          validity: res.data[i].validity == null ? '去抵现' : '已使用',
          val: res.data[i].validity == 0 ? 0 : 1,
          date: utils.compareTime(res.data[i].date, '', ''),
          note: res.data[i].validity == null ? '持续有效' : '优惠券已过期',
          // 3位 + 7位 + 3位 + 券id + 用户id
          code: giv.substring(rect % 28, rect % 28 + 3) + rect.toString(16).substring(1) + rec.substring(rect % 28 + 3, rect % 28 + 6) + parseInt(res.data[i].id).toString(16) + '%' + appData.user.id,
        });
      }
      console.log(intro)
      if (res.data.length) that.setData({footTitle: '没有更多优惠券了'});
      that.setData({intro: intro, loading: false});
    })
  },

  onShareAppMessage: function () {

  }
})